using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WLYT.Application.Interfaces;
using WLYT.Domain.ApiModel;
using WLYT.Domain.Model;
using WLYT.Repository;

namespace WLYT.Application.Services
{
    public class MaterialService : BaseService<Material>, IMaterialService
    {
        public ISqlSugarRepository<Material> MaterialRepository;
        public ISqlSugarRepository<Supplier> SupplierRepository;
        public ISqlSugarRepository<OrderItem> OrderItemRepository;
        public MaterialService(ISqlSugarRepository<Material> materialRepository,
            ISqlSugarRepository<Supplier> supplierRepository,
            ISqlSugarRepository<OrderItem> orderItemRepository) : base(materialRepository)
        {
            MaterialRepository = materialRepository;
            SupplierRepository = supplierRepository;
            OrderItemRepository = orderItemRepository;
        }

        public async Task<Tuple<IList<Material>, int>> GetModelsByPageAsync(string itemCode, string itemName, string maderCode, string originalCode, int pageSize, int pageIndex)
        {
            Expressionable<Material> exp = Expressionable.Create<Material>();
            exp.AndIF(!string.IsNullOrEmpty(itemCode), p => p.ItemCode.Contains(itemCode));
            exp.AndIF(!string.IsNullOrEmpty(originalCode), p => p.OriginalCode.Contains(originalCode));
            exp.AndIF(!string.IsNullOrEmpty(itemName), p => p.ItemName.Contains(itemName));
            exp.AndIF(!string.IsNullOrEmpty(maderCode), p => p.MaderCode.Contains(maderCode));
            return await GetPageListAsync(exp, pageSize, pageIndex, p => p.Id);
        }

        public async Task<ApiResult> SyncMaterial(List<MaterialModel> list)
        {
            ApiResult result = new ApiResult();
            try
            {
                await _baseRepository.AsTenant().BeginTranAsync();
                foreach (var item in list)
                {
                    var model = await MaterialRepository.GetFirstAsync(p => p.ItemCode == item.item_code);
                    int itemMinPack = 0;
                    try
                    {
                        itemMinPack = Convert.ToInt32(Convert.ToDouble(item.item_min_pack));
                    }
                    catch
                    {
                    }
                    if (model == null)
                    {
                        await MaterialRepository.InsertAsync(new Material
                        {
                            ItemCode = item.item_code,
                            CreateTime = DateTime.Now,
                            ItemMinPack = itemMinPack,
                            ItemName = item.item_name ?? "",
                            MaderCode = item.mader_code ?? "",
                            MaderName = item.mader_name ?? "",
                            OriginalCode = item.original_code ?? "",
                            UnitCode = item.unit_code ?? "",
                            SMLevel = item.sm_level ?? "",
                        });
                    }
                    else
                    {
                        model.UpdateTime = DateTime.Now;
                        model.ItemMinPack = itemMinPack;
                        model.ItemName = item.item_name ?? "";
                        model.MaderCode = item.mader_code ?? "";
                        model.MaderName = item.mader_name ?? "";
                        model.OriginalCode = item.original_code ?? "";
                        model.UnitCode = item.unit_code ?? "";
                        model.SMLevel = item.sm_level ?? "";
                        await MaterialRepository.UpdateAsync(model);
                    }
                    if (!string.IsNullOrEmpty(item.mader_name))
                    {
                        await OrderItemRepository.UpdateAsync(p => new OrderItem { MaderName = item.mader_name }, p => p.ItemCode == item.item_code && p.MaderName == "");
                    }
                    if (!string.IsNullOrEmpty(item.original_code))
                    {
                        await OrderItemRepository.UpdateAsync(p => new OrderItem { OriginalCode = item.original_code }, p => p.ItemCode == item.item_code && p.OriginalCode == "");
                    }
                }

                await _baseRepository.AsTenant().CommitTranAsync();
            }
            catch (Exception ex)
            {
                await _baseRepository.AsTenant().RollbackTranAsync();
                result.Code = 500;
                result.Message = ex.Message;
            }
            return result;
        }
    }
}